/*
 * Copyright (c) 2010, 2012 Luan Nguyen Thanh. All rights reserved.
 * 
 * This code is written by Luan Nguyen Thanh, with any partners involved.
 * The unauthorized use of this code should not be permitted!
 */
package MyIM.client.view.component;

import MyIM.client.view.lnf.SegoeFont;
import MyIM.component.Friend;
import MyIM.component.Group;
import MyIM.utilities.Util;
import java.awt.Color;
import java.awt.Component;
import java.awt.FlowLayout;
import java.awt.Image;
import javax.swing.ImageIcon;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTree;
import javax.swing.tree.DefaultMutableTreeNode;
import javax.swing.tree.DefaultTreeCellRenderer;
import javax.swing.tree.TreeCellRenderer;

/**
 * The panel to display Contacts with avatar and current status.
 *
 * @author Luan Nguyen Thanh <tklarryonline@gmail.com>
 */
public class FriendListItem extends JPanel implements TreeCellRenderer {

    private static final Color ONLINE = Color.BLACK;
    private static final Color OFFLINE = Color.LIGHT_GRAY;
    private ImageIcon avatar;
    private String username, displayName;
    private Friend friend;
    private Group group;

    /**
     * Creates a default FriendListItem with default avatar.
     */
    public FriendListItem() {
    }

    public FriendListItem(Friend friend) {
        this.friend = friend;
        initFriend();
    }

    public FriendListItem(Group group) {
        this.group = group;
        initGroup();
    }

    private void initFriend() {
        // Initializes components
        avatar = new ImageIcon(friend.getAvatar().getImage().getScaledInstance(
                24, 24, Image.SCALE_SMOOTH));

        // Prepares layout
        setLayout(new FlowLayout(FlowLayout.LEADING));
        setOpaque(true);

        add(new JLabel(avatar, JLabel.LEADING));

        ImageIcon imgStatus;
        Color colorStatus;
        if (friend.isStatus()) {
            imgStatus = Util.makeIcon("available");
            colorStatus = ONLINE;
        } else {
            imgStatus = Util.makeIcon("offline");
            colorStatus = OFFLINE;
        }

        add(new JLabel(imgStatus, JLabel.LEADING));

        JLabel lblName = new JLabel(friend.getDisplayName(), JLabel.LEADING);
        lblName.setForeground(colorStatus);
        add(lblName);
    }

    private void initGroup() {
        // Prepares layout
        setLayout(new FlowLayout(FlowLayout.LEADING));
        setOpaque(true);

        JLabel label = new JLabel(group.getGroupName(), JLabel.LEADING);
        label.setFont(SegoeFont.getSegoeUIFont("normal"));
        add(label);
    }

    @Override
    public Component getTreeCellRendererComponent(JTree tree, Object value,
            boolean selected, boolean expanded, boolean leaf, int row,
            boolean hasFocus) {
        Component returnValue = null;
        DefaultTreeCellRenderer defaultRenderer = new DefaultTreeCellRenderer();
        Color bgSelectionColor = defaultRenderer.getBackgroundSelectionColor();
        Color bgNonSelectionColor = defaultRenderer.
                getBackgroundNonSelectionColor();

        if (value != null && value instanceof DefaultMutableTreeNode) {
            // Gets the object inside
            Object userObject = ((DefaultMutableTreeNode) value).getUserObject();
            FriendListItem item;

            if (userObject instanceof Friend) {
                Friend friendObj = (Friend) userObject;
                item = new FriendListItem(friendObj);
            } else if (userObject instanceof Group) {
                Group groupObj = (Group) userObject;
                item = new FriendListItem(groupObj);

            } else {
                item = new FriendListItem();
            }

            if (selected) {
                item.setBackground(bgSelectionColor);
            } else {
                item.setBackground(bgNonSelectionColor);
            }

            returnValue = item;
        }

        if (returnValue == null) {
            returnValue = defaultRenderer.getTreeCellRendererComponent(tree,
                    value, leaf, expanded, leaf, row, hasFocus);
        }

        return returnValue;
    }
}
